package com.szbber.medical.board.viewModel

import android.content.Context
import androidx.lifecycle.MutableLiveData
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.gson.Gson
import com.szbber.commen.api.Repository
import com.szbber.commen.api.launch
import com.szbber.commen.base.BaseViewModel
import com.szbber.commen.bean.DeptBean
import com.szbber.commen.bean.ScheduleBean
import com.szbber.commen.dialog.DialogUtils
import com.szbber.commen.utils.dataStoreRead
import com.szbber.commen.utils.dataStoreSave

class ScheduleFragmentViewModel : BaseViewModel() {

    val deptBean = MutableLiveData<DeptBean.Data>()
    fun getDeptName() = launchUI {
        deptBean.value = Gson().fromJson<DeptBean.Data>(dataStoreRead<String>("DeptBean"),
            DeptBean.Data::class.java)
    }

    val scheduleBean = MutableLiveData<ScheduleBean>()
    fun getSchedule(context: Context?) = launch(context, {
        val deptCode = dataStoreRead<String>("deptCode")
        val queue = arrayOf("deptCode", deptCode)
        scheduleBean.value = Repository.getSchedule(context, *queue)
    })

    fun getPatientList(context: Context?,careLabelId:String?) = launch(context, {
        val deptCode = dataStoreRead<String>("deptCode")
        val queue = arrayOf("deptCode", deptCode)
        val data = Repository.getPatientList(context, *queue)
        DialogUtils.showPatientListBean(context, data) {
            addCareLevel(context, it.patientListBean?.cure_no, it.patientListBean?.bed_code, careLabelId)
        }
    })

    fun addCareLevel(context: Context?,cureNo: String?, bedCode:String?, careLabelId:String?) = launch(context,{
        val deptCode =  dataStoreRead<String>("deptCode")
        val queue = arrayOf("patientId", cureNo, "careLabelId",careLabelId,"bedCode", bedCode,"deptCode", deptCode)
        Repository.addCareLevel(context, *queue)
        getSchedule(context)
    })

    fun deleteCareLevel(context: Context?,id:String ) = launch(context,{
        val queue = arrayOf("id", id)
        Repository.deleteCareLevel(context, *queue)
        getSchedule(context)
    })

}